/*
 * @Author: shunhua.liang
 * @Date: 2022-04-11 16:39:31
 * @LastEditors: shunhua.liang
 * @LastEditTime: 2022-04-13 12:15:03
 * @Description:
 */
// import {  constantRoutes } from '@/router'
import { getAllPadsUserButtonPermissions ,stocksBusinessGroupConfigEnable,stocksBusinessGroup} from '@/api/user'
const state = {
  // 按钮信息
  authList: [],
  businessGroupShow:false,
  businessGroupData:[],
  businessUnitCodeDetail:'',// 详情里面的业务单元编码
}

const mutations = {
  SET_AUTHLIST: (state, authList) => {
    state.authList = authList
  },
  SET_BUSINESSGROUPSHOW: (state, fal) => {
    state.businessGroupShow = fal
  },
  SET_BUSINESSGROUPDATA: (state, list) => {
    state.businessGroupData = list
  },
  SET_BUSINESSUNITCODEDETAIL: (state, code) => {
    state.businessUnitCodeDetail = code
  }
}

const actions = {
  getAllPadsUserButtonPermissions ({ commit }, id) {
    return new Promise((resolve, reject) => {
      getAllPadsUserButtonPermissions()
        .then(res => {
          const { code, data, message } = res.data
          if (+code === 200) {
            const authList = data.filter(item => item.parentId === id)
            console.log(authList, 'authList')
            console.log(id, 'id')
            commit('SET_AUTHLIST', authList || [])
            resolve()
          } else {
            this.$notify({
              title: '提示',
              message
            })
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },

  stocksBusinessGroupConfigEnable ({ commit }, id) {
    return new Promise((resolve, reject) => {
      stocksBusinessGroupConfigEnable().then(res => {
          const { code, data, message } = res.data
          if (+code === 200) {
            commit('SET_BUSINESSGROUPSHOW',  !!res.data.data)
            if(!!res.data.data){
              stocksBusinessGroup().then(res=>{
                commit('SET_BUSINESSGROUPDATA',  res.data.data || [])
                resolve()
              })
            }else{
              resolve()
            }
          } else {
            this.$notify({
              title: '提示',
              message
            })
          }
        }).catch(err => {
          reject(err)
        })
    })
  },
  setbusinessUnitCodeDetail({ commit }, code) {
    return new Promise((resolve, reject) => {
       commit('SET_BUSINESSUNITCODEDETAIL',  code)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
